// User Model - 数据模型层

const db = require('@/database/index')

/**
 * 根据用户名查询用户
 */
exports.findByUsername = (username) => {
  return new Promise((resolve, reject) => {
    const sql = 'SELECT * FROM userInfo WHERE username = ?'
    db.query(sql, [username], (err, results) => {
      if (err) return reject(err)
      resolve(results)
    })
  })
}

/**
 * 创建新用户
 */
exports.create = (userData) => {
  return new Promise((resolve, reject) => {
    const sql = 'INSERT INTO userInfo SET ?'
    db.query(sql, userData, (err, results) => {
      if (err) return reject(err)
      resolve(results)
    })
  })
}


/** 
 * 根据 id 查询用户
 */
exports.findById = (id) => {
  return new Promise((resolve, reject) => {
    const sql = "select * from userInfo where id = ?"
    db.query(sql, [id], (err, results) => {
      if (err) {
        return reject(err)
      }
      resolve(results)
    })
  })
}